package com.lanju.app.service;

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.lanju.app.message.bean.B107Request;
import com.lanju.app.message.bean.B107Response;
import com.lanju.app.message.bean.B107Response.DetailBean;
import com.lanju.entity.Order;
import com.lanju.entity.User;

/**
 * 通过订单id获取订单详情
 * @author fly
 *
 */
@Service("B107Service")
public class B107Service extends BaseService<B107Request,B107Response>{

	@Override
	public B107Response doService(B107Request request, User u) throws Exception {
		B107Response res = new B107Response();		
		if(StringUtils.isBlank(request.getOrderId())){
			res.setMessage("订单号参数错误！");
			return res;
		}
		Order order = dao.get(Order.class, request.getOrderId());
		if(order==null){
			res.setMessage("未找到订单！");
			return res;
		}
//		if(StringUtils.isNotBlank(order.getBusiId())){
//			if(!order.getBusiId().equals(request.getToken())){
//				res.setMessage("您无权查看该订单！");
//				return res;
//			}
//		}		
		res.setShopName(order.getShopName()==null?"":order.getShopName());
		res.setAddress(order.getAddress()==null?"":order.getAddress());
		res.setAmount(order.getAmount()==null?0:order.getAmount().floatValue());
		res.setCarNum(order.getCarNum()==null?"":order.getCarNum());
		res.setTel(order.getTel()==null?"":order.getTel());
		res.setFox(order.getFox()==null?"":order.getFox());
		res.setBusiName(order.getBusiName()==null?"":order.getBusiName());
		
		String sql = " SELECT prod_name AS prodName, price, amount, number, o.unit unit, view_unit viewUnit FROM order_detail o LEFT JOIN product_sku b ON o.sku_id=b.id WHERE order_id=? ";
		ArrayList<String> param = new ArrayList<>();
		param.add(order.getId());			
		res.setDetails(dao.list(sql.toString(), DetailBean.class,  param.toArray()));		
		res.setSuccess(true);
		return res;
	}
}
